<?php
/**
 * Created by WarCraft3
 * User: WarCraft3
 * Date: 2021/8/30
 * Time: 12:17
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

class Shop extends Api
{
    private $_uid;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected function _initialize()
    {
        parent::_initialize();
        $jwt = $this->request->header('Authorization');
        if ($jwt) {
            $this->check_token($jwt);
            $this->_uid = $this->_token['uid'];
        } else {
            $this->success('缺少token', '', '401');
        }
    }

    /*
 * 商城封面轮播图
 * */
    public function banner()
    {
        $shop_banner = Db::name('shop_banner')->select()->toArray();
        $this->success('商城封面轮播图',$shop_banner);
    }
    /*
     * 获取 vip券列表
     * */
    public function lists()
    {
        $keyword = $this->request->param('keyword');
        if(!empty($keyword)){
            $goods = Db::name('shop_goods')
                ->whereLike('name','%'.$keyword.'%','like')
                ->where('switch','1')
                ->select();
        }else{
            $goods = Db::name('shop_goods')
                ->where('switch','1')
                ->select();
        }
        $this->success('成功',$goods);
    }

    /*
     * 用户请求兑换某个优惠券
     * */
    public function exchange()
    {
        $uid = $this->_uid;
        $goods_id = $this->request->param('goods_id');
        $in['shop_goods_id'] = $goods_id;
        $in['auser_id'] = $uid;
        $order_no = mt_rand(10,100000).'_'.date('YmdHis').'_'.$uid;
        $in['no'] = $order_no;
        $in['ctime'] = date('Y-m-d H:i:s',time());
        $in['point'] = Db::name('shop_goods')->where('id',$goods_id)->value('point');
        $auser=Db::name('auser')->where('id',$uid)->find();
        if($in['point']>$auser['ji_fen']){
            $this->error('积分不够');
        }
        $ret = Db::name('shop_record')->insert($in);
        //减少用户积分
        Db::name('auser')->where('id',$uid)->setDec('ji_fen',$in['point']);
        if ($ret)$this->success('成功');
        $this->error('失败');
    }


    /*
     * 积分商城兑换记录
     * */
    public function recs(){
        $uid = $this->_uid;
        $record = Db::name('shop_record')
            ->alias('r')
            ->join('shop_goods g','g.id = r.shop_goods_id')
            ->where('r.auser_id',$uid)
            ->field('g.name,g.image,g.point,g.lastdate,r.no,r.ctime,r.image as qrcode')
            ->select();
        if ($record){
            $this->success('成功',$record);
        }
        $this->error('无记录');
    }

    /*
    * 积分记录
    * */
    public  function jfrec()
    {
        $ret = [];
        $user = Db::name('auser')->where('id',$this->_uid)->field('ji_fen,all_fen')->find();
        $ret['ji_fen'] = $user['ji_fen'];
        $ret['all_fen'] = $user['all_fen'];
        $inc = Db::name('point')->order('ctime desc')->field('jifen,ctime,avideo_id,exam_id')->where('auser_id',$this->_uid)->where('type','0')->select()->each(function ($item){
            $item['video_name'] = Db::name('avideo')->where('id',$item['avideo_id'])->value('name');
            $item['exam_name'] = Db::name('exam')->where('id',$item['exam_id'])->value('name');
            $item['point'] = $item['jifen'];
            unset($item['avideo_id']);
            unset($item['exam_id']);
            unset($item['jifen']);
            return $item;
        })->toArray();
        $dec = Db::name('shop_record')->order('ctime desc')->where('auser_id',$this->_uid)->field('ctime,no,point')->select()->toArray();;
        //改成统一的格式
        $ret['inc']=$inc;
        $ret['dec']=$dec;
        if(!empty($inc)){
            foreach ($inc as $k=>$v){
                $inc[$k]['status']='add';
                if(empty($inc[$k]['video_name'])){
                    $inc[$k]['name']=$inc[$k]['exam_name'];
                }else{
                    $inc[$k]['name']=$inc[$k]['video_name'];
                }
            }
        }
        if(!empty($dec)){
            foreach ($dec as $k=>$v){
                $dec[$k]['status']='subtract';
            }
        }
        if(!empty($dec)&&!empty($inc)){
            $all=array_merge($dec,$inc);
        }elseif(!empty($dec)&&empty($inc)){
            $all=$dec;
        }elseif(empty($dec)&&!empty($inc)){
            $all=$inc;
        }else{
            $all=[];
        }
        $all = $this->arraySortByField($all, 'ctime', 'DESC');
        $ret['all']=$all;
        $this->success('ok',$ret);
    }
    /*
    * 学分记录
    * */
    public  function xfrec()
    {
        $ret = [];
        $user = Db::name('auser')->where('id',$this->_uid)->field('xue_fen,all_fen')->find();
        $ret['xue_fen'] = $user['xue_fen'];
        $ret['all_fen'] = $user['all_fen'];
        $inc = Db::name('point')->order('ctime desc')->field('xuefen,ctime,avideo_id,exam_id')->where('auser_id',$this->_uid)->where('type','1')->select()->each(function ($item){
            $item['video_name'] = Db::name('avideo')->where('id',$item['avideo_id'])->value('name');
            $item['exam_name'] = Db::name('exam')->where('id',$item['exam_id'])->value('name');
            $item['point'] = $item['xuefen'];
            unset($item['avideo_id']);
            unset($item['exam_id']);
            unset($item['xuefen']);
            return $item;
        })->toArray();
        //改成统一的格式
        $ret['inc']=$inc;
        if(!empty($inc)){
            foreach ($inc as $k=>$v){
                $inc[$k]['status']='add';
                if(empty($inc[$k]['video_name'])){
                    $inc[$k]['name']=$inc[$k]['exam_name'];
                }else{
                    $inc[$k]['name']=$inc[$k]['video_name'];
                }
            }
        }
        $all = $this->arraySortByField($inc, 'ctime', 'DESC');
        $ret['all']=$all;
        $this->success('ok',$ret);
    }
    function arraySortByField($arr = [], $field = "", $order = 'asc')
    {
        $order = strtoupper($order);
        if ('ASC' == $order) {
            array_multisort(array_column($arr, $field), SORT_ASC, $arr);
        }
        if ('DESC' == $order) {
            array_multisort(array_column($arr, $field), SORT_DESC, $arr);
        }
        return $arr;
    }
}